#' ---
#' title: "Cheap Tweets"
#' author: "Benjamin Norwood Harris and Erik Lin-Greenberg"
#' date: "January 2024"
#' output: pdf_document
#' ---
#' 
#' # Intro 
#' 
## -----------------------------------------------------------------------------
#Read in needed packages 
library(stargazer)
library(lmtest)


#' 
#' 
#' 
## -----------------------------------------------------------------------------
#set WD  

setwd("/Users/harri/Dropbox (MIT)/Audience Costs and Twitter/SUBMISSION 2023/ISQ Final Accepted/Replication Materials")

#' 
#' # Public Sample 
#' 
## -----------------------------------------------------------------------------
DF_PublicSample <-read.csv("Data, Public_Sample.csv")

#' 
#' 
#' 
#' ## Demographic Table Stats 
#' 
#' ### Gender 
## -----------------------------------------------------------------------------


table(DF_PublicSample$Q.Female)

length(na.omit(DF_PublicSample$Q.Female[DF_PublicSample$Q.Female==0]))/length(DF_PublicSample$Q.Female) #males 
sum(na.omit(DF_PublicSample$Q.Female))/length(DF_PublicSample$Q.Female) #females 

sum(is.na(DF_PublicSample$Q.Female))/length(DF_PublicSample$Q.Female) #NAs/other


#' 
#' ### Race 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_PublicSample$Q.White))/length((DF_PublicSample$Q.White))
sum(na.omit(DF_PublicSample$Q.Black))/length((DF_PublicSample$Q.Black))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_PublicSample$Q.AIorAN))/length((DF_PublicSample$Q.AIorAN))
sum(na.omit(DF_PublicSample$Q.Asian))/length((DF_PublicSample$Q.Asian))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_PublicSample$Q.NHorPI))/length((DF_PublicSample$Q.NHorPI))
sum(na.omit(DF_PublicSample$Q.Hispanic))/length((DF_PublicSample$Q.Hispanic))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_PublicSample$Q.Mixed))/length((DF_PublicSample$Q.Mixed))
sum(na.omit(DF_PublicSample$Q.Other))/length((DF_PublicSample$Q.Other))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_PublicSample$Q.Other_Mixed))/length((DF_PublicSample$Q.Other_Mixed))
sum(is.na(DF_PublicSample$Q.Race))/length(DF_PublicSample$Q.Race)




#' 
#' ### Age 
#' 
## -----------------------------------------------------------------------------
summary(DF_PublicSample$Q.Age)

#' 
#' ### Education 
#' 
## -----------------------------------------------------------------------------


table(DF_PublicSample$Q.Education)

sum((na.omit(DF_PublicSample$Q.HighSchool)))/length((DF_PublicSample$Q.HighSchool))
sum((na.omit(DF_PublicSample$Q.Bach)))/length((DF_PublicSample$Q.Bach))

sum(is.na(DF_PublicSample$Q.Education))/length(DF_PublicSample$Q.Education)


#' ### Income 
#' 
## -----------------------------------------------------------------------------
table(DF_PublicSample$Q.Income)


length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==1]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==2]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==3]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==4]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==5]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==6]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==7]))/length(DF_PublicSample$Q.Income)
length(na.omit(DF_PublicSample$Q.Income[DF_PublicSample$Q.Income==8]))/length(DF_PublicSample$Q.Income)



sum(is.na(DF_PublicSample$Q.Income))/length(DF_PublicSample$Q.Income)


#' ### Political ID 
#' 
## -----------------------------------------------------------------------------


table(DF_PublicSample$Q.Political_ID)

sum(na.omit(DF_PublicSample$Q.Liberal))/length((DF_PublicSample$Q.Liberal))
sum(na.omit(DF_PublicSample$Q.Moderate))/length((DF_PublicSample$Q.Moderate))
sum(na.omit(DF_PublicSample$Q.Conservative))/length((DF_PublicSample$Q.Conservative))


sum(is.na(DF_PublicSample$Q.Political_ID))/length(DF_PublicSample$Q.Political_ID)


#' ### Veteran 
#' 
## -----------------------------------------------------------------------------

table(DF_PublicSample$Q.Veteran)
length(na.omit(DF_PublicSample$Q.Veteran[DF_PublicSample$Q.Veteran==0]))/length(DF_PublicSample$Q.Veteran)
length(na.omit(DF_PublicSample$Q.Veteran[DF_PublicSample$Q.Veteran==1]))/length(DF_PublicSample$Q.Veteran)

sum(is.na(DF_PublicSample$Q.Veteran))/length(DF_PublicSample$Q.Veteran)



#' 
#' ## Analysis 
#' 
#' ### Credibility 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


Cred_Full_1 <- Credibility ~ 0 + as.factor(Tweet) 
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF_PublicSample)

summary(lm_Cred_Full_1)

length(na.omit(DF_PublicSample$Credibility[DF_PublicSample$Tweet==1]))
length(na.omit(DF_PublicSample$Credibility[DF_PublicSample$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 
#let's do a couple different models 

##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

Cred_Form_1 <- Credibility ~ Tweet   

## Model 5: Factor Demographics 

Cred_Form_5 <- 
  Credibility ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran

#regression 

Cred_1 <- lm(Cred_Form_1, 
             data = DF_PublicSample, 
             na.action=na.omit)

Cred_5 <- lm(Cred_Form_5, 
             data = DF_PublicSample, 
             na.action=na.omit)

Cred_7 <- lm(Cred_Form_7, 
             data = DF_PublicSample, 
             na.action=na.omit)

library(stargazer)


stargazer(Cred_1, Cred_5, Cred_7, title = "Perceived Credibility with Covariates", no.space = TRUE)


#' 
#' 
## -----------------------------------------------------------------------------

# Credibility ATE Graph 


Cred_Alone <- as.data.frame(matrix(data = c("credibility", coeftest(Cred_5)[2, 1:2],"Yes", 
                                   "credibility", coeftest(Cred_1)[2, 1:2], "No"), 
                                   ncol = 4, byrow = TRUE))

colnames(Cred_Alone) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Cred_Alone$estimate <- as.numeric(Cred_Alone$estimate)
Cred_Alone$se <- as.numeric(Cred_Alone$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Cred_Alone$ci <- Cred_Alone$se*q

library(ggplot2)
## graph time 
pd <- position_dodge(0.5)

ggplot(Cred_Alone, aes(x = controls, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Controls Present?") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()



#ggtitle("Credibility, ATE of Tweet Medium, Public")

#' 
#' ### Manipulation Check 
#' 
## -----------------------------------------------------------------------------
##### stargazer tables for MC

# Threat/Treatment Check 

## Model 1: No Demographics 

Threat_Form_1 <- Threat_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Threat_Form_5 <- Threat_Check_Binary ~ Tweet  + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 



## Model 7: Slightly simplified for the Appendix 

Threat_Form_7 <- Threat_Check_Binary ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran

#regression 

Threat_1 <- lm(Threat_Form_1, 
             data = DF_PublicSample, 
             na.action=na.omit)

Threat_5 <- lm(Threat_Form_5, 
             data = DF_PublicSample, 
             na.action=na.omit)

Threat_7 <- lm(Threat_Form_7, 
             data = DF_PublicSample, 
             na.action=na.omit)


stargazer(Threat_1, Threat_5, Threat_7, title = "Treatment Check Question with Covariates", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
# Target Check 

## Model 1: No Demographics 

Target_Form_1 <- Target_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Target_Form_5 <- Target_Check_Binary ~ Tweet  +  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 



## Model 7: Slightly simplified for the Appendix 

Target_Form_7 <- Target_Check_Binary ~ Tweet  + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran


#regression 

Target_1 <- lm(Target_Form_1, 
             data = DF_PublicSample, 
             na.action=na.omit)

Target_5 <- lm(Target_Form_5, 
             data = DF_PublicSample, 
             na.action=na.omit)

Target_7 <- lm(Target_Form_7, 
             data = DF_PublicSample, 
             na.action=na.omit)


stargazer(Target_1, Target_5, Target_7, title = "Target Check Question with Covariates", no.space = TRUE)


#' 
#' 
## -----------------------------------------------------------------------------
# Support Check 

## Model 1: No Demographics 

Support_Form_1 <- Support_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Support_Form_5 <- Support_Check_Binary ~ Tweet  + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 



## Model 7: Slightly simplified for the Appendix 

Support_Form_7 <- Support_Check_Binary ~ Tweet  + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran


#regression 

Support_1 <- lm(Support_Form_1, 
             data = DF_PublicSample, 
             na.action=na.omit)

Support_5 <- lm(Support_Form_5, 
             data = DF_PublicSample, 
             na.action=na.omit)

Support_7 <- lm(Support_Form_7, 
             data = DF_PublicSample, 
             na.action=na.omit)


stargazer(Support_1, Support_5, Support_7, title = "Support Check Question with Covariates", no.space = TRUE)


#' 
#' 
## -----------------------------------------------------------------------------
# Sea Check 

## Model 1: No Demographics 

Sea_Form_1 <- Sea_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Sea_Form_5 <- Sea_Check_Binary ~ Tweet  + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 



## Model 7: Slightly simplified for the Appendix 

Sea_Form_7 <- Sea_Check_Binary ~ Tweet  + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran


#regression 

Sea_1 <- lm(Sea_Form_1, 
             data = DF_PublicSample, 
             na.action=na.omit)

Sea_5 <- lm(Sea_Form_5, 
             data = DF_PublicSample, 
             na.action=na.omit)

Sea_7 <- lm(Sea_Form_7, 
             data = DF_PublicSample, 
             na.action=na.omit)


stargazer(Sea_1, Sea_5, Sea_7, title = "Sea Check Question with Covariates", no.space = TRUE)


#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
#simple manipulation check percentages 
#using DF_PublicSample 

## overall 
sum(DF_PublicSample$Threat_Check_Binary)/length(DF_PublicSample$Threat_Check_Binary)
sum(DF_PublicSample$Target_Check_Binary)/length(DF_PublicSample$Target_Check_Binary)
sum(DF_PublicSample$Support_Check_Binary)/length(DF_PublicSample$Support_Check_Binary)
sum(DF_PublicSample$Sea_Check_Binary)/length(DF_PublicSample$Sea_Check_Binary)

#' 
#' 
## -----------------------------------------------------------------------------
## Tweet Treatments 
sum(DF_PublicSample$Threat_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])/length(DF_PublicSample$Threat_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])
sum(DF_PublicSample$Target_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])/length(DF_PublicSample$Target_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])
sum(DF_PublicSample$Support_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])/length(DF_PublicSample$Support_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])
sum(DF_PublicSample$Sea_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])/length(DF_PublicSample$Sea_Check_Binary[DF_PublicSample$Treatment==1 | DF_PublicSample$Treatment==2])

#' 
#' 
## -----------------------------------------------------------------------------
## Official Statement Treatments 
sum(DF_PublicSample$Threat_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])/length(DF_PublicSample$Threat_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])
sum(DF_PublicSample$Target_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])/length(DF_PublicSample$Target_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])
sum(DF_PublicSample$Support_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])/length(DF_PublicSample$Support_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])
sum(DF_PublicSample$Sea_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])/length(DF_PublicSample$Sea_Check_Binary[DF_PublicSample$Treatment==3 | DF_PublicSample$Treatment==4])

#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
## graphs for manipulation check 

#put all the names, estimates, SEs, and behavior into one df for graphing 
Results_DF <- as.data.frame(matrix(data = c("Treatment Check", coeftest(Threat_1)[2, 1:2],"no", 
                                   "Treatment Check", coeftest(Threat_5)[2, 1:2], "yes",
                                   "Target Check", coeftest(Target_1)[2, 1:2],"no", 
                                   "Target Check", coeftest(Target_5)[2, 1:2], "yes",
                                   "Support Check", coeftest(Support_1)[2, 1:2], "no",
                                   "Support Check", coeftest(Support_5)[2, 1:2], "yes",
                                   "Sea Check", coeftest(Sea_1)[2, 1:2], "no",
                                   "Sea Check", coeftest(Sea_5)[2, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

colnames(Results_DF) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF$dv <- factor(Results_DF$dv, levels = c("Treatment Check", "Target Check", "Support Check", "Sea Check"))
Results_DF$estimate <- as.numeric(Results_DF$estimate)
Results_DF$se <- as.numeric(Results_DF$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF$ci <- Results_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

ggplot(Results_DF, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Tweet Imagery ATE by MC or AC Question") +


#' 
#'  
#' 
## -----------------------------------------------------------------------------
## mean and se tables for threat check 

####Threat/Treatment Check 
Threat_Full_1 <- Threat_Check_Binary ~ 0 + as.factor(Tweet) 


lm_Threat_Full_1 <- lm(Threat_Full_1, 
                     data = DF_PublicSample)

summary(lm_Threat_Full_1)
length(na.omit(DF_PublicSample$Threat_Check_Binary[DF_PublicSample$Tweet==1]))
length(na.omit(DF_PublicSample$Threat_Check_Binary[DF_PublicSample$Tweet==0]))


#' 
#' 
## -----------------------------------------------------------------------------
##### Target Check 
Target_Full_1 <- Target_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Target_Full_1 <- lm(Target_Full_1, 
                     data = DF_PublicSample)

summary(lm_Target_Full_1)
length(na.omit(DF_PublicSample$Target_Check_Binary[DF_PublicSample$Tweet==1]))
length(na.omit(DF_PublicSample$Target_Check_Binary[DF_PublicSample$Tweet==0]))

#' 
#' 
## -----------------------------------------------------------------------------
##### Support Check 

Support_Full_1 <- Support_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Support_Full_1 <- lm(Support_Full_1, 
                     data = DF_PublicSample)

summary(lm_Support_Full_1)
length(na.omit(DF_PublicSample$Support_Check_Binary[DF_PublicSample$Tweet==1]))
length(na.omit(DF_PublicSample$Support_Check_Binary[DF_PublicSample$Tweet==0]))

#' 
#' 
## -----------------------------------------------------------------------------
##### Sea Check 

Sea_Full_1 <- Sea_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Sea_Full_1 <- lm(Sea_Full_1, 
                     data = DF_PublicSample)

summary(lm_Sea_Full_1)

length(na.omit(DF_PublicSample$Sea_Check_Binary[DF_PublicSample$Tweet==1]))
length(na.omit(DF_PublicSample$Sea_Check_Binary[DF_PublicSample$Tweet==0]))

#' 
#' 
#' 
#' 
#' ### Interaction Effects 
#' 
#' 
## -----------------------------------------------------------------------------

# looking at interactions between partisanship and tweet 
# and between age and Tweet 
# and between veteran and Tweet

### two new formulas for interaction. 8 is interaction with partisanship. 9 is for age 

Cred_Form_8 <- Credibility ~ Tweet*Q.Liberal + Tweet*Q.Conservative + Q.Female + relevel(as.factor(Q.Race), ref = 1) + 
    Q.Age + Q.Bach + Q.Income  +  Q.Veteran
  
Cred_Form_9 <- Credibility ~ Tweet*Q.Age + Q.Female + relevel(as.factor(Q.Race), ref = 1) + 
    Q.Bach + Q.Income + Q.Conservative + Q.Liberal + 
    Q.Veteran

Cred_Form_V <- Credibility ~ Tweet*Q.Veteran + Q.Female + relevel(as.factor(Q.Race), ref = 1) + 
    Q.Bach + Q.Income + Q.Conservative + Q.Liberal + Q.Age





Cred_8 <- lm(Cred_Form_8, data = DF_PublicSample) 
Cred_9 <- lm(Cred_Form_9, data = DF_PublicSample) 
Cred_V <- lm(Cred_Form_V, data = DF_PublicSample) 


stargazer(Cred_8, Cred_9, Cred_V, title = "Crediblity with Interaction Terms with Covariates", no.space = TRUE)




#' 
#' ## AC Sensitivity Analysis  
#' 
#' ### Just three ACs   
#' 
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------
DF4 <- subset(DF_PublicSample, DF_PublicSample$Target_Check_Binary==1)
DF5 <- subset(DF4, DF4$Support_Check_Binary==1)
DF6 <- subset(DF5, DF5$Sea_Check_Binary==1)

#' 
#' #### Analysis 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


AC.Cred_Full_1 <- Credibility ~ 0 + as.factor(Tweet) 
AC.lm_Cred_Full_1 <- lm(AC.Cred_Full_1, 
                     data = DF6)

summary(AC.lm_Cred_Full_1)

length(na.omit(DF6$Credibility[DF6$Tweet==1]))
length(na.omit(DF6$Credibility[DF6$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 


##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

AC.Cred_Form_1 <- Credibility ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

AC.Cred_Form_5 <- 
  Credibility ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 
  

## Model 7: Slightly simplified for the Appendix 

AC.Cred_Form_7 <- Credibility ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran

#regression 

AC.Cred_1 <- lm(AC.Cred_Form_1, 
             data = DF6, 
             na.action=na.omit)

AC.Cred_5 <- lm(AC.Cred_Form_5, 
             data = DF6, 
             na.action=na.omit)

AC.Cred_7 <- lm(AC.Cred_Form_7, 
             data = DF6, 
             na.action=na.omit)

library(stargazer)


stargazer(AC.Cred_1, AC.Cred_5, AC.Cred_7, title = "Perceived Credibility, AC Questions Correct", no.space = TRUE)


#' 
#' ### Just with MC
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------

DF7 <- subset(DF_PublicSample, DF_PublicSample$Threat_Check_Binary==1)

#' 
#' #### Analysis 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


MC.Cred_Full_1 <- Credibility ~ 0 + as.factor(Tweet) 
MC.lm_Cred_Full_1 <- lm(MC.Cred_Full_1, 
                     data = DF7)

summary(MC.lm_Cred_Full_1)

length(na.omit(DF7$Credibility[DF7$Tweet==1]))
length(na.omit(DF7$Credibility[DF7$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 


##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

MC.Cred_Form_1 <- Credibility ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

MC.Cred_Form_5 <- 
  Credibility ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 
  

## Model 7: Slightly simplified for the Appendix 

MC.Cred_Form_7 <- Credibility ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran

#regression 

MC.Cred_1 <- lm(MC.Cred_Form_1, 
             data = DF7, 
             na.action=na.omit)

MC.Cred_5 <- lm(MC.Cred_Form_5, 
             data = DF7, 
             na.action=na.omit)

MC.Cred_7 <- lm(MC.Cred_Form_7, 
             data = DF7, 
             na.action=na.omit)

library(stargazer)


stargazer(MC.Cred_1, MC.Cred_5, MC.Cred_7, title = "Perceived Credibility, MC Question Correct", no.space = TRUE)


#' 
#' # Expert Experiment
#' 
#' 
## -----------------------------------------------------------------------------
DF_ExpertSample <-read.csv("Data, Expert_Sample.csv")

#' 
#' ## Analysis 
#' 
## -----------------------------------------------------------------------------

# regression formulas 

## credibility 

### Model 1: No Demographics 

Cred_form_1 <- Credibility ~ Tweet

### Model 5: All demographics, and ordered factors 

Cred_form_5 <- Credibility ~ Tweet + AfterKabul + female + Veteran_Binary + Gov_Binary  + bach +
  as.factor(Country) + Age + as.factor(Current_Affiliation) + factor(Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5))  
  


#' 
#' 
## -----------------------------------------------------------------------------

# Regressions 

library(stats)
library(lmtest)

##credibility 
lm_cred_5 <- lm(formula = Cred_form_5, data = DF_ExpertSample) 
lm_cred_1 <- lm(formula = Cred_form_1, data = DF_ExpertSample) 



Cred_DF <- as.data.frame(matrix(data = c("credibility", coeftest(lm_cred_5)[2, 1:2],"Yes", 
                                   "credibility", coeftest(lm_cred_1)[2, 1:2], "No"), 
                                   ncol = 4, byrow = TRUE))

colnames(Cred_DF) <- c("dv", "estimate", "se", "controls")


#making into correct operators 
Cred_DF$estimate <- as.numeric(Cred_DF$estimate)
Cred_DF$se <- as.numeric(Cred_DF$se)


#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))

Cred_DF$ci <- Cred_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

## cred graph 
ggplot(Cred_DF, aes(x = controls, y = estimate))  + 
  geom_point(aes(), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Controls Present?") + ylab("ATE") + 
  ggtitle("Credibility, ATE of Tweet Medium, Experts") + geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1)) + coord_flip()


#' 
#' 
## -----------------------------------------------------------------------------
# let's repeat the graphs, but break them out by country 

DF_US <- subset(DF_ExpertSample, DF_ExpertSample$Country==1)
DF_Singapore <- subset(DF_ExpertSample, DF_ExpertSample$Country==2)
DF_India <- subset(DF_ExpertSample, DF_ExpertSample$Country==3)

# Credibility 

##have to make a new model 5 formula without country 
Cred_form_5_NC <- Credibility ~ Tweet + AfterKabul + female + Veteran_Binary + 
    Gov_Binary + highschool + bach + Age + 
    as.factor(Current_Affiliation) + factor(Political_ID, ordered = TRUE, 
    levels = c(1, 2, 3, 4, 5))

# Regressions 

lm_cred_5_US <- lm(formula = Cred_form_5_NC, data = DF_US) 
lm_cred_1_US <- lm(formula = Cred_form_1, data = DF_US) 
lm_cred_5_Singapore <- lm(formula = Cred_form_5_NC, data = DF_Singapore) 
lm_cred_1_Singapore <- lm(formula = Cred_form_1, data = DF_Singapore) 
lm_cred_5_India <- lm(formula = Cred_form_5_NC, data = DF_India) 
lm_cred_1_India <- lm(formula = Cred_form_1, data = DF_India) 


Cred_DF_Country <- as.data.frame(matrix(data = c("credibility", coeftest(lm_cred_5_US)[2, 1:2],"Yes", "US",  
                                   "credibility", coeftest(lm_cred_1_US)[2, 1:2], "No", "US", 
                                   "credibility", coeftest(lm_cred_5_Singapore)[2, 1:2],"Yes", "Singapore",  
                                   "credibility", coeftest(lm_cred_1_Singapore)[2, 1:2], "No", "Singapore", 
                                   "credibility", coeftest(lm_cred_5_India)[2, 1:2],"Yes", "India",  
                                   "credibility", coeftest(lm_cred_1_India)[2, 1:2], "No", "India"),
                                   ncol = 5, byrow = TRUE))


colnames(Cred_DF_Country) <- c("dv", "estimate", "se", "controls", "country")


#making into correct operators 
Cred_DF_Country$estimate <- as.numeric(Cred_DF_Country$estimate)
Cred_DF_Country$se <- as.numeric(Cred_DF_Country$se)
Cred_DF_Country$country <- factor(Cred_DF_Country$country)


#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))

Cred_DF_Country$ci <- Cred_DF_Country$se*q



## cred graph 
ggplot(Cred_DF_Country, aes(x = controls, y = estimate, color = country))  + 
  geom_point(aes(color=country, shape=country), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Controls Present?") + ylab("ATE") + 
  ggtitle("Credibility, ATE of Tweet Medium by Country, Experts") + geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1)) + coord_flip()

#' 
#' 
#' 
## -----------------------------------------------------------------------------
#stargazer of the above 
library(stargazer)

stargazer(lm_cred_1, lm_cred_5, title = "Credibility, Full Elite Sample", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
stargazer(lm_cred_1_US, lm_cred_5_US, title = "Credibility, US Elite Sample", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
stargazer(lm_cred_1_Singapore, lm_cred_5_Singapore, title = "Credibility, India Elite Sample", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
stargazer(lm_cred_1_India, lm_cred_5_India, title = "Credibility, Singapore Elite Sample", no.space = TRUE)


#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
#regressions for means and SEs

##because of small size, I have to get the basic values without covariates 

#full sample 
Cred_Full_1 <- Credibility ~ 0 + as.factor(Tweet)  



lm_Cred_Full_1 <- lm(Cred_Full_1, data = DF_ExpertSample)


summary(lm_Cred_Full_1)

length(na.omit(DF_ExpertSample$Credibility[DF_ExpertSample$Tweet==1]))
length(na.omit(DF_ExpertSample$Credibility[DF_ExpertSample$Tweet==0]))



#' 
## -----------------------------------------------------------------------------
# by country 

Cred_Full_1_NC <- Credibility ~ 0 + as.factor(Tweet) 


## US 

lm_Cred_Full_1_NC_US <- lm(Cred_Full_1_NC, data = DF_US)


summary(lm_Cred_Full_1_NC_US)



length(na.omit(DF_US$Credibility[DF_US$Tweet==1]))
length(na.omit(DF_US$Credibility[DF_US$Tweet==0]))

#' 
#' 
## -----------------------------------------------------------------------------
## Singapore
lm_Cred_Full_1_NC_Singapore <- lm(Cred_Full_1_NC, data = DF_Singapore)

mean(DF_Singapore$Credibility[DF_Singapore$Tweet==1])

summary(lm_Cred_Full_1_NC_Singapore)


length(na.omit(DF_Singapore$Credibility[DF_Singapore$Tweet==1]))
length(na.omit(DF_Singapore$Credibility[DF_Singapore$Tweet==0]))


#' 
#' 
## -----------------------------------------------------------------------------
## India 
lm_Cred_Full_1_NC_India <- lm(Cred_Full_1_NC, data = DF_India)


summary(lm_Cred_Full_1_NC_India)

length(na.omit(DF_India$Credibility[DF_India$Tweet==1]))
length(na.omit(DF_India$Credibility[DF_India$Tweet==0]))


#' 
#' # Follow On 1, President and Language 
#' 
#' 
## -----------------------------------------------------------------------------
DF_Follow1 <-read.csv("Data, Follow_1_President.csv")

#' 
#' 
#' 
#' 
#' ## Demographic Table Stats 
#' 
#' 
## -----------------------------------------------------------------------------

### demographics 

table(DF_Follow1$Q.Female)

length(na.omit(DF_Follow1$Q.Female[DF_Follow1$Q.Female==0]))/length(DF_Follow1$Q.Female)
sum(na.omit(DF_Follow1$Q.Female))/length(DF_Follow1$Q.Female)

sum(is.na(DF_Follow1$Q.Female))/length(DF_Follow1$Q.Female)


#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow1$Q.White))/length((DF_Follow1$Q.White))
sum(na.omit(DF_Follow1$Q.Black))/length((DF_Follow1$Q.Black))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow1$Q.AIorAN))/length((DF_Follow1$Q.AIorAN))
sum(na.omit(DF_Follow1$Q.Asian))/length((DF_Follow1$Q.Asian))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow1$Q.NHorPI))/length((DF_Follow1$Q.NHorPI))
sum(na.omit(DF_Follow1$Q.Hispanic))/length((DF_Follow1$Q.Hispanic))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow1$Q.Mixed))/length((DF_Follow1$Q.Mixed))
sum(na.omit(DF_Follow1$Q.Other))/length((DF_Follow1$Q.Other))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow1$Q.Other_Mixed))/length((DF_Follow1$Q.Other_Mixed))
sum(is.na(DF_Follow1$Q.Race))/length(DF_Follow1$Q.Race)




#' 
#' 
## -----------------------------------------------------------------------------
summary(DF_Follow1$Q.Age)

#' 
#' 
## -----------------------------------------------------------------------------


table(DF_Follow1$Q.Education)

sum((na.omit(DF_Follow1$Q.HighSchool)))/length((DF_Follow1$Q.HighSchool))
sum((na.omit(DF_Follow1$Q.Bach)))/length((DF_Follow1$Q.Bach))

sum(is.na(DF_Follow1$Q.Education))/length(DF_Follow1$Q.Education)


#' 
#' 
## -----------------------------------------------------------------------------
table(DF_Follow1$Q.Income)


length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==1]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==2]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==3]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==4]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==5]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==6]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==7]))/length(DF_Follow1$Q.Income)
length(na.omit(DF_Follow1$Q.Income[DF_Follow1$Q.Income==8]))/length(DF_Follow1$Q.Income)



sum(is.na(DF_Follow1$Q.Income))/length(DF_Follow1$Q.Income)


#' 
#' 
## -----------------------------------------------------------------------------


table(DF_Follow1$Q.Political_ID)

sum(na.omit(DF_Follow1$Q.Liberal))/length((DF_Follow1$Q.Liberal))
sum(na.omit(DF_Follow1$Q.Moderate))/length((DF_Follow1$Q.Moderate))
sum(na.omit(DF_Follow1$Q.Conservative))/length((DF_Follow1$Q.Conservative))


sum(is.na(DF_Follow1$Q.Political_ID))/length(DF_Follow1$Q.Political_ID)


#' 
#' 
## -----------------------------------------------------------------------------

table(DF_Follow1$Q.Veteran)
length(na.omit(DF_Follow1$Q.Veteran[DF_Follow1$Q.Veteran==0]))/length(DF_Follow1$Q.Veteran)
length(na.omit(DF_Follow1$Q.Veteran[DF_Follow1$Q.Veteran==1]))/length(DF_Follow1$Q.Veteran)

sum(is.na(DF_Follow1$Q.Veteran))/length(DF_Follow1$Q.Veteran)



#' 
## -----------------------------------------------------------------------------
table(DF_Follow1$Q.Twitter_Use )



length(na.omit(DF_Follow1$Q.Twitter_Use[DF_Follow1$Q.Twitter_Use==1]))/length(DF_Follow1$Q.Twitter_Use)
length(na.omit(DF_Follow1$Q.Twitter_Use[DF_Follow1$Q.Twitter_Use==2]))/length(DF_Follow1$Q.Twitter_Use)
length(na.omit(DF_Follow1$Q.Twitter_Use[DF_Follow1$Q.Twitter_Use==3]))/length(DF_Follow1$Q.Twitter_Use)
length(na.omit(DF_Follow1$Q.Twitter_Use[DF_Follow1$Q.Twitter_Use==4]))/length(DF_Follow1$Q.Twitter_Use)
length(na.omit(DF_Follow1$Q.Twitter_Use[DF_Follow1$Q.Twitter_Use==5]))/length(DF_Follow1$Q.Twitter_Use)
sum(is.na(DF_Follow1$Q.Twitter_Use))/length(DF_Follow1$Q.Twitter_Use)


#' 
#' 
#' 
#' ## Analysis 
#' 
#' ### Mean Estimates with Standard Error 
#' 
#' 
#' 
## -----------------------------------------------------------------------------

Cred_Full_1 <- Credibility ~ 0 + as.factor(TweetMedium)
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF_Follow1)

summary(lm_Cred_Full_1)

length(DF_Follow1$Credibility[DF_Follow1$TweetMedium==1])
length(DF_Follow1$Credibility[DF_Follow1$TweetMedium==2])
length(DF_Follow1$Credibility[DF_Follow1$TweetMedium==3])
length(DF_Follow1$Credibility[DF_Follow1$TweetMedium==4])
length(DF_Follow1$Credibility[DF_Follow1$TweetMedium==5])
length(DF_Follow1$Credibility[DF_Follow1$TweetMedium==6])


#' 
#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 


## Model 1: No Demographics 

Cred_Form_1 <- Credibility ~ Tweet + Informal + Trump

## Model 5: Factor Demographics 

Cred_Form_5 <- Credibility ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some


#regression 



Cred_1 <- lm(Cred_Form_1, 
             data = DF_Follow1, 
             na.action=na.omit)
Cred_5 <- lm(Cred_Form_5, 
             data = DF_Follow1, 
             na.action=na.omit)
Cred_7 <- lm(Cred_Form_7, 
             data = DF_Follow1, 
             na.action=na.omit)


stargazer(Cred_1, Cred_5, Cred_7, title = "Credibility, Follow-On", no.space = TRUE)



#' 
#' 
## -----------------------------------------------------------------------------
#graph time 


Cred_DF <- as.data.frame(matrix(data = c("Tweet", coeftest(Cred_5)[2, 1:2],"Yes", 
                                   "Tweet", coeftest(Cred_1)[2, 1:2], "No", 
                                   "Informal", coeftest(Cred_5)[3, 1:2],"Yes",  
                                   "Informal", coeftest(Cred_1)[3, 1:2], "No", 
                                   "Trump", coeftest(Cred_5)[4, 1:2],"Yes",  
                                   "Trump", coeftest(Cred_1)[4, 1:2], "No"),
                                   ncol = 4, byrow = TRUE))


colnames(Cred_DF) <- c("IV", "estimate", "se", "controls")


#making into correct operators 
Cred_DF$estimate <- as.numeric(Cred_DF$estimate)
Cred_DF$se <- as.numeric(Cred_DF$se)


#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))

Cred_DF$ci <- Cred_DF$se*q

library(ggplot2)
## graph time 
pd <- position_dodge(0.5)

## cred graph 
ggplot(Cred_DF, aes(x = controls, y = estimate, color = IV))  + 
  geom_point(aes(color=IV, shape=IV), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Controls Present?") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1)) + coord_flip()


#ggtitle("ATE on Credibility, all IVs")

#' 
#' 
#' ### Manipulation Check 
#' 
## -----------------------------------------------------------------------------
##### stargazer tables for MC
#### note: this version has removed the "backsdown" binary variable 

# Threat/Treatment Check 

## Model 1: No Demographics 

Threat_Form_1 <- Threat_Check_Binary ~ Tweet + Informal + Trump    

  
## Model 5: Factor Demographics 

Threat_Form_5 <- Threat_Check_Binary ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Threat_Form_7 <- Threat_Check_Binary ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Threat_1 <- lm(Threat_Form_1, 
             data = DF_Follow1, 
             na.action=na.omit)

Threat_5 <- lm(Threat_Form_5, 
             data = DF_Follow1, 
             na.action=na.omit)

Threat_7 <- lm(Threat_Form_7, 
             data = DF_Follow1, 
             na.action=na.omit)


stargazer(Threat_1, Threat_5, Threat_7, title = "Treatment Check Question with Covariates", no.space = TRUE)

#' 
#' 
#' 
## -----------------------------------------------------------------------------
# President Check 

## Model 1: No Demographics 

President_Form_1 <- President_Check_Binary ~ Tweet + Informal + Trump    

  
## Model 5: Factor Demographics 

President_Form_5 <- President_Check_Binary ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

President_Form_7 <- President_Check_Binary ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

President_1 <- lm(President_Form_1, 
             data = DF_Follow1, 
             na.action=na.omit)

President_5 <- lm(President_Form_5, 
             data = DF_Follow1, 
             na.action=na.omit)

President_7 <- lm(President_Form_7, 
             data = DF_Follow1, 
             na.action=na.omit)


stargazer(President_1, President_5, President_7, title = "President Check Question with Covariates", no.space = TRUE)

#' 
#' 
#' 
## -----------------------------------------------------------------------------
# Target Check 

## Model 1: No Demographics 

Target_Form_1 <- Target_Check_Binary ~ Tweet + Informal + Trump    

  
## Model 5: Factor Demographics 

Target_Form_5 <- Target_Check_Binary ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Target_Form_7 <- Target_Check_Binary ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Target_1 <- lm(Target_Form_1, 
             data = DF_Follow1, 
             na.action=na.omit)

Target_5 <- lm(Target_Form_5, 
             data = DF_Follow1, 
             na.action=na.omit)

Target_7 <- lm(Target_Form_7, 
             data = DF_Follow1, 
             na.action=na.omit)


stargazer(Target_1, Target_5, Target_7, title = "Target Check Question with Covariates", no.space = TRUE)

#' 
## -----------------------------------------------------------------------------
# Support Check 

## Model 1: No Demographics 

Support_Form_1 <- Support_Check_Binary ~ Tweet + Informal + Trump    

  
## Model 5: Factor Demographics 

Support_Form_5 <- Support_Check_Binary ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Support_Form_7 <- Support_Check_Binary ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Support_1 <- lm(Support_Form_1, 
             data = DF_Follow1, 
             na.action=na.omit)

Support_5 <- lm(Support_Form_5, 
             data = DF_Follow1, 
             na.action=na.omit)

Support_7 <- lm(Support_Form_7, 
             data = DF_Follow1, 
             na.action=na.omit)


stargazer(Support_1, Support_5, Support_7, title = "Support Check Question with Covariates", no.space = TRUE)

#' 
#' 
#' 
## -----------------------------------------------------------------------------
# Sea Check 

## Model 1: No Demographics 

Sea_Form_1 <- Sea_Check_Binary ~ Tweet + Informal + Trump    

  
## Model 5: Factor Demographics 

Sea_Form_5 <- Sea_Check_Binary ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Sea_Form_7 <- Sea_Check_Binary ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Sea_1 <- lm(Sea_Form_1, 
             data = DF_Follow1, 
             na.action=na.omit)

Sea_5 <- lm(Sea_Form_5, 
             data = DF_Follow1, 
             na.action=na.omit)

Sea_7 <- lm(Sea_Form_7, 
             data = DF_Follow1, 
             na.action=na.omit)


stargazer(Sea_1, Sea_5, Sea_7, title = "Sea Check Question with Covariates", no.space = TRUE)

#' 
#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
## more detail on manipulation check 

## ATE of Tweet Treatment 

#put all the names, estimates, SEs, and behavior into one df for graphing 
Results_DF <- as.data.frame(matrix(data = c("Treatment Check", coeftest(Threat_1)[2, 1:2],"no", 
                                   "Treatment Check", coeftest(Threat_5)[2, 1:2], "yes",
                                   "President Check", coeftest(President_1)[2, 1:2],"no", 
                                   "President Check", coeftest(President_5)[2, 1:2], "yes",
                                   "Target Check", coeftest(Target_1)[2, 1:2],"no", 
                                   "Target Check", coeftest(Target_5)[2, 1:2], "yes",
                                   "Support Check", coeftest(Support_1)[2, 1:2], "no",
                                   "Support Check", coeftest(Support_5)[2, 1:2], "yes",
                                   "Sea Check", coeftest(Sea_1)[2, 1:2], "no",
                                   "Sea Check", coeftest(Sea_5)[2, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

colnames(Results_DF) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF$dv <- factor(Results_DF$dv, levels = c("Treatment Check", "President Check", "Target Check", "Support Check", "Sea Check"))
Results_DF$estimate <- as.numeric(Results_DF$estimate)
Results_DF$se <- as.numeric(Results_DF$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF$ci <- Results_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

ggplot(Results_DF, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Tweet Imagery ATE by MC or AC Question") +


#' 
## -----------------------------------------------------------------------------
## more detail on manipulation check 

## ATE of Informal Language Treatment 

#put all the names, estimates, SEs, and behavior into one df for graphing 
Results_DF <- as.data.frame(matrix(data = c("Treatment Check", coeftest(Threat_1)[3, 1:2],"no", 
                                   "Treatment Check", coeftest(Threat_5)[3, 1:2], "yes",
                                   "President Check", coeftest(President_1)[3, 1:2],"no", 
                                   "President Check", coeftest(President_5)[3, 1:2], "yes",
                                   "Target Check", coeftest(Target_1)[3, 1:2],"no", 
                                   "Target Check", coeftest(Target_5)[3, 1:2], "yes",
                                   "Support Check", coeftest(Support_1)[3, 1:2], "no",
                                   "Support Check", coeftest(Support_5)[3, 1:2], "yes",
                                   "Sea Check", coeftest(Sea_1)[3, 1:2], "no",
                                   "Sea Check", coeftest(Sea_5)[3, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

colnames(Results_DF) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF$dv <- factor(Results_DF$dv, levels = c("Treatment Check", "President Check", "Target Check", "Support Check", "Sea Check"))
Results_DF$estimate <- as.numeric(Results_DF$estimate)
Results_DF$se <- as.numeric(Results_DF$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF$ci <- Results_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

ggplot(Results_DF, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Informal Language ATE by MC or AC Question") +


#' 
#' 
## -----------------------------------------------------------------------------
## more detail on manipulation check 

## ATE of Trump as President Treatment 

#put all the names, estimates, SEs, and behavior into one df for graphing 
Results_DF <- as.data.frame(matrix(data = c("Treatment Check", coeftest(Threat_1)[4, 1:2],"no", 
                                   "Treatment Check", coeftest(Threat_5)[4, 1:2], "yes",
                                   "President Check", coeftest(President_1)[4, 1:2],"no", 
                                   "President Check", coeftest(President_5)[4, 1:2], "yes",
                                   "Target Check", coeftest(Target_1)[4, 1:2],"no", 
                                   "Target Check", coeftest(Target_5)[4, 1:2], "yes",
                                   "Support Check", coeftest(Support_1)[4, 1:2], "no",
                                   "Support Check", coeftest(Support_5)[4, 1:2], "yes",
                                   "Sea Check", coeftest(Sea_1)[4, 1:2], "no",
                                   "Sea Check", coeftest(Sea_5)[4, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

colnames(Results_DF) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF$dv <- factor(Results_DF$dv, levels = c("Treatment Check", "President Check", "Target Check", "Support Check", "Sea Check"))
Results_DF$estimate <- as.numeric(Results_DF$estimate)
Results_DF$se <- as.numeric(Results_DF$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF$ci <- Results_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

ggplot(Results_DF, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Trump as President ATE by MC or AC Question") +


#' 
#' 
#' #### Mean and SEs 
#' 
## -----------------------------------------------------------------------------
## mean and se tables for threat check 

####Threat/Treatment Check 
Threat_Full_1 <- Threat_Check_Binary ~ 0 + as.factor(TweetMedium) 


lm_Threat_Full_1 <- lm(Threat_Full_1, 
                     data = DF_Follow1)

summary(lm_Threat_Full_1)
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$TweetMedium==1]) #statement
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$TweetMedium==2])
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$TweetMedium==3])
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$TweetMedium==4]) #statement 
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$TweetMedium==5])
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$TweetMedium==6])

#' 
#' 
## -----------------------------------------------------------------------------
####Threat/Treatment Check 
Threat_Full_1 <- Threat_Check_Binary ~ 0 + as.factor(Tweet) 


lm_Threat_Full_1 <- lm(Threat_Full_1, 
                     data = DF_Follow1)

summary(lm_Threat_Full_1)
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$Tweet==1])
length(DF_Follow1$Threat_Check_Binary[DF_Follow1$Tweet==0])



#' 
#' 
#' 
## -----------------------------------------------------------------------------
##### President Check 
President_Full_1 <- President_Check_Binary ~ 0 + as.factor(TweetMedium)  

lm_President_Full_1 <- lm(President_Full_1, 
                     data = DF_Follow1)

summary(lm_President_Full_1)
length(DF_Follow1$President_Check_Binary[DF_Follow1$Tweet==0])
length(DF_Follow1$President_Check_Binary[DF_Follow1$Tweet==1])


#' 
## -----------------------------------------------------------------------------
##### Target Check 
Target_Full_1 <- Target_Check_Binary ~ 0 + as.factor(TweetMedium) 

lm_Target_Full_1 <- lm(Target_Full_1, 
                     data = DF_Follow1)

summary(lm_Target_Full_1)
length(DF_Follow1$Target_Check_Binary[DF_Follow1$TweetMedium==1])
length(DF_Follow1$Target_Check_Binary[DF_Follow1$TweetMedium==2])
length(DF_Follow1$Target_Check_Binary[DF_Follow1$TweetMedium==3])
length(DF_Follow1$Target_Check_Binary[DF_Follow1$TweetMedium==4])
length(DF_Follow1$Target_Check_Binary[DF_Follow1$TweetMedium==5])
length(DF_Follow1$Target_Check_Binary[DF_Follow1$TweetMedium==6])

#' 
#' 
## -----------------------------------------------------------------------------
##### Support Check 

Support_Full_1 <- Support_Check_Binary ~ 0 + as.factor(TweetMedium) 

lm_Support_Full_1 <- lm(Support_Full_1, 
                     data = DF_Follow1)

summary(lm_Support_Full_1)
length(DF_Follow1$Support_Check_Binary[DF_Follow1$TweetMedium==1])
length(DF_Follow1$Support_Check_Binary[DF_Follow1$TweetMedium==2])
length(DF_Follow1$Support_Check_Binary[DF_Follow1$TweetMedium==3])
length(DF_Follow1$Support_Check_Binary[DF_Follow1$TweetMedium==4])
length(DF_Follow1$Support_Check_Binary[DF_Follow1$TweetMedium==5])
length(DF_Follow1$Support_Check_Binary[DF_Follow1$TweetMedium==6])

#' 
#' 
## -----------------------------------------------------------------------------
##### Sea Check 

Sea_Full_1 <- Sea_Check_Binary ~ 0 + as.factor(TweetMedium) 

lm_Sea_Full_1 <- lm(Sea_Full_1, 
                     data = DF_Follow1)

summary(lm_Sea_Full_1)
length(DF_Follow1$Sea_Check_Binary[DF_Follow1$TweetMedium==1])
length(DF_Follow1$Sea_Check_Binary[DF_Follow1$TweetMedium==2])
length(DF_Follow1$Sea_Check_Binary[DF_Follow1$TweetMedium==3])
length(DF_Follow1$Sea_Check_Binary[DF_Follow1$TweetMedium==4])
length(DF_Follow1$Sea_Check_Binary[DF_Follow1$TweetMedium==5])
length(DF_Follow1$Sea_Check_Binary[DF_Follow1$TweetMedium==6])

#' 
#' 
#' 
#' 
#' ### Interaction Effects 
#' 
## -----------------------------------------------------------------------------

# looking at interactions between Trump and both informal language and Tweet
# looking at interactions between informal language and Tweet and between Tweet and Twitter Usage
# looking at interactions between Trump and political ideology   


### two new formulas for interaction. 8 is interaction with partisanship. 9 is for age 

Cred_Form_8 <- Credibility ~ Tweet*Trump + Informal*Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

Cred_Form_9 <- Credibility ~ Tweet*Q.Twitter_Some + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran 


Cred_Form_10 <- Credibility ~ Tweet + Informal + Trump*Q.Conservative + Trump*Q.Liberal  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Veteran + Q.Twitter_Some




Cred_8 <- lm(Cred_Form_8, data = DF_Follow1) 
Cred_9 <- lm(Cred_Form_9, data = DF_Follow1) 
Cred_10 <- lm(Cred_Form_10, data = DF_Follow1) 


stargazer(Cred_8, Cred_9, Cred_10, title = "Crediblity with Interaction Terms and Covariates", no.space = TRUE)




#' 
#' ## AC Sensitivity Analysis  
#' 
#' ### Just ACs   
#' 
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------
DF4 <- subset(DF_Follow1, DF_Follow1$Target_Check_Binary==1)
DF5 <- subset(DF4, DF4$Support_Check_Binary==1)
DF6 <- subset(DF5, DF5$Sea_Check_Binary==1)



#' 
#' ### Analysis 
#' 
## -----------------------------------------------------------------------------

Cred_Full_1 <- Credibility ~ 0 + as.factor(TweetMedium)
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF6)

summary(lm_Cred_Full_1)

length(DF6$Credibility[DF6$TweetMedium==1])
length(DF6$Credibility[DF6$TweetMedium==2])
length(DF6$Credibility[DF6$TweetMedium==3])
length(DF6$Credibility[DF6$TweetMedium==4])
length(DF6$Credibility[DF6$TweetMedium==5])
length(DF6$Credibility[DF6$TweetMedium==6])


#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 


## Model 1: No Demographics 

Cred_Form_1 <- Credibility ~ Tweet + Informal + Trump

## Model 5: Factor Demographics 

Cred_Form_5 <- Credibility ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some


#regression 



Cred_1 <- lm(Cred_Form_1, 
             data = DF6, 
             na.action=na.omit)
Cred_5 <- lm(Cred_Form_5, 
             data = DF6, 
             na.action=na.omit)
Cred_7 <- lm(Cred_Form_7, 
             data = DF6, 
             na.action=na.omit)


stargazer(Cred_1, Cred_5, Cred_7, title = "Credibility, AC Questions Correct", no.space = TRUE)

#' 
#' 
#' ### Just MCs   
#' 
#' 
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------

DF7 <- subset(DF_Follow1, DF_Follow1$President_Check_Binary==1)
DF8 <- subset(DF7, DF7$Threat_Check_Binary==1)


#' 
#' 
#' #### Analysis 
#' 
#' 
## -----------------------------------------------------------------------------

Cred_Full_1 <- Credibility ~ 0 + as.factor(TweetMedium)
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF8)

summary(lm_Cred_Full_1)

length(DF8$Credibility[DF8$TweetMedium==1])
length(DF8$Credibility[DF8$TweetMedium==2])
length(DF8$Credibility[DF8$TweetMedium==3])
length(DF8$Credibility[DF8$TweetMedium==4])
length(DF8$Credibility[DF8$TweetMedium==5])
length(DF8$Credibility[DF8$TweetMedium==6])


#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 


## Model 1: No Demographics 

Cred_Form_1 <- Credibility ~ Tweet + Informal + Trump

## Model 5: Factor Demographics 

Cred_Form_5 <- Credibility ~ Tweet + Informal + Trump   + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility ~ Tweet + Informal + Trump  + 
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some


#regression 



Cred_1 <- lm(Cred_Form_1, 
             data = DF8, 
             na.action=na.omit)
Cred_5 <- lm(Cred_Form_5, 
             data = DF8, 
             na.action=na.omit)
Cred_7 <- lm(Cred_Form_7, 
             data = DF8, 
             na.action=na.omit)


stargazer(Cred_1, Cred_5, Cred_7, title = "Credibility, MC Questions Correct", no.space = TRUE)

#' 
#' # Follow On 2, Iran 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
DF_Follow2 <-read.csv("Data, Follow_2_Iran.csv")

#' 
#' 
#' 
#' 
#' ## Demographic Table Stats 
#' 
#' 
## -----------------------------------------------------------------------------

### demographics 

table(DF_Follow2$Q.Female)

length(na.omit(DF_Follow2$Q.Female[DF_Follow2$Q.Female==0]))/length(DF_Follow2$Q.Female)
sum(na.omit(DF_Follow2$Q.Female))/length(DF_Follow2$Q.Female)
sum(is.na(DF_Follow2$Q.Female))/length(DF_Follow2$Q.Female)


#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow2$Q.White))/length((DF_Follow2$Q.White))
sum(na.omit(DF_Follow2$Q.Black))/length((DF_Follow2$Q.Black))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow2$Q.AIorAN))/length((DF_Follow2$Q.AIorAN))
sum(na.omit(DF_Follow2$Q.Asian))/length((DF_Follow2$Q.Asian))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow2$Q.NHorPI))/length((DF_Follow2$Q.NHorPI))
sum(na.omit(DF_Follow2$Q.Hispanic))/length((DF_Follow2$Q.Hispanic))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow2$Q.Mixed))/length((DF_Follow2$Q.Mixed))
sum(na.omit(DF_Follow2$Q.Other))/length((DF_Follow2$Q.Other))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow2$Q.Other_Mixed))/length((DF_Follow2$Q.Other_Mixed))
sum(is.na(DF_Follow2$Q.Race))/length(DF_Follow2$Q.Race)




#' 
#' 
## -----------------------------------------------------------------------------
summary(DF_Follow2$Q.Age)

#' 
#' 
## -----------------------------------------------------------------------------


table(DF_Follow2$Q.Education)

sum((na.omit(DF_Follow2$Q.HighSchool)))/length((DF_Follow2$Q.HighSchool))
sum((na.omit(DF_Follow2$Q.Bach)))/length((DF_Follow2$Q.Bach))

sum(is.na(DF_Follow2$Q.Education))/length(DF_Follow2$Q.Education)


#' 
#' 
## -----------------------------------------------------------------------------
table(DF_Follow2$Q.Income)


length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==1]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==2]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==3]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==4]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==5]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==6]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==7]))/length(DF_Follow2$Q.Income)
length(na.omit(DF_Follow2$Q.Income[DF_Follow2$Q.Income==8]))/length(DF_Follow2$Q.Income)



sum(is.na(DF_Follow2$Q.Income))/length(DF_Follow2$Q.Income)


#' 
#' 
## -----------------------------------------------------------------------------


table(DF_Follow2$Q.Political_ID)

sum(na.omit(DF_Follow2$Q.Liberal))/length((DF_Follow2$Q.Liberal))
sum(na.omit(DF_Follow2$Q.Moderate))/length((DF_Follow2$Q.Moderate))
sum(na.omit(DF_Follow2$Q.Conservative))/length((DF_Follow2$Q.Conservative))


sum(is.na(DF_Follow2$Q.Political_ID))/length(DF_Follow2$Q.Political_ID)


#' 
#' 
## -----------------------------------------------------------------------------

table(DF_Follow2$Q.Veteran)
length(na.omit(DF_Follow2$Q.Veteran[DF_Follow2$Q.Veteran==0]))/length(DF_Follow2$Q.Veteran)
length(na.omit(DF_Follow2$Q.Veteran[DF_Follow2$Q.Veteran==1]))/length(DF_Follow2$Q.Veteran)

sum(is.na(DF_Follow2$Q.Veteran))/length(DF_Follow2$Q.Veteran)



#' 
## -----------------------------------------------------------------------------

#Twitter_Use 

table(DF_Follow2$Q.Twitter_Use)



length(na.omit(DF_Follow2$Q.Twitter_Use[DF_Follow2$Q.Twitter_Use==1]))/length(DF_Follow2$Q.Twitter_Use)
length(na.omit(DF_Follow2$Q.Twitter_Use[DF_Follow2$Q.Twitter_Use==2]))/length(DF_Follow2$Q.Twitter_Use)
length(na.omit(DF_Follow2$Q.Twitter_Use[DF_Follow2$Q.Twitter_Use==3]))/length(DF_Follow2$Q.Twitter_Use)
length(na.omit(DF_Follow2$Q.Twitter_Use[DF_Follow2$Q.Twitter_Use==4]))/length(DF_Follow2$Q.Twitter_Use)
length(na.omit(DF_Follow2$Q.Twitter_Use[DF_Follow2$Q.Twitter_Use==5]))/length(DF_Follow2$Q.Twitter_Use)
sum(is.na(DF_Follow2$Q.Twitter_Use))/length(DF_Follow2$Q.Twitter_Use)


#' 
#' 
## -----------------------------------------------------------------------------
colnames(DF_Follow2)

#' 
#' ## Analysis 
#' 
#' ### Credibility 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


Cred_Full_1 <- Credibility_Iran ~ 0 + as.factor(Tweet) 
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF_Follow2)

summary(lm_Cred_Full_1)

length(na.omit(DF_Follow2$Credibility_Iran[DF_Follow2$Tweet==1]))
length(na.omit(DF_Follow2$Credibility_Iran[DF_Follow2$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 

##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

Cred_Form_1 <- Credibility_Iran ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

Cred_Form_5 <- 
  Credibility_Iran ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))
  

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility_Iran ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Cred_1 <- lm(Cred_Form_1, 
             data = DF_Follow2, 
             na.action=na.omit)

Cred_5 <- lm(Cred_Form_5, 
             data = DF_Follow2, 
             na.action=na.omit)

Cred_7 <- lm(Cred_Form_7, 
             data = DF_Follow2, 
             na.action=na.omit)

library(stargazer)


stargazer(Cred_1, Cred_5, Cred_7, title = "Perceived Credibility with Covariates", no.space = TRUE)


#' 
## -----------------------------------------------------------------------------

#Credibility not separated 



Cred_Alone <- as.data.frame(matrix(data = c("credibility", coeftest(Cred_5)[2, 1:2],"Yes", 
                                   "credibility", coeftest(Cred_1)[2, 1:2], "No"), 
                                   ncol = 4, byrow = TRUE))

colnames(Cred_Alone) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Cred_Alone$estimate <- as.numeric(Cred_Alone$estimate)
Cred_Alone$se <- as.numeric(Cred_Alone$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Cred_Alone$ci <- Cred_Alone$se*q

library(ggplot2)
## graph time 
pd <- position_dodge(0.5)

ggplot(Cred_Alone, aes(x = controls, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Controls Present?") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Credibility, ATE of Tweet Medium, Iran")

#' 
#' 
#' ### Manipulation Check 
#' 
## -----------------------------------------------------------------------------
##### stargazer tables for MC
#### note: this version has removed the "backsdown" binary variable 

# Threat/Treatment Check 

## Model 1: No Demographics 

Threat_Form_1 <- Threat_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Threat_Form_5 <- Threat_Check_Binary ~ Tweet  + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))



## Model 7: Slightly simplified for the Appendix 

Threat_Form_7 <- Threat_Check_Binary ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Threat_1 <- lm(Threat_Form_1, 
             data = DF_Follow2, 
             na.action=na.omit)

Threat_5 <- lm(Threat_Form_5, 
             data = DF_Follow2, 
             na.action=na.omit)

Threat_7 <- lm(Threat_Form_7, 
             data = DF_Follow2, 
             na.action=na.omit)


stargazer(Threat_1, Threat_5, Threat_7, title = "Treatment Check Question with Covariates", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
# Target Check 

## Model 1: No Demographics 

Target_Form_1 <- Target_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Target_Form_5 <- Target_Check_Binary ~ Tweet  +  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))



## Model 7: Slightly simplified for the Appendix 

Target_Form_7 <- Target_Check_Binary ~ Tweet  + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Target_1 <- lm(Target_Form_1, 
             data = DF_Follow2, 
             na.action=na.omit)

Target_5 <- lm(Target_Form_5, 
             data = DF_Follow2, 
             na.action=na.omit)

Target_7 <- lm(Target_Form_7, 
             data = DF_Follow2, 
             na.action=na.omit)


stargazer(Target_1, Target_5, Target_7, title = "Target Check Question with Covariates", no.space = TRUE)


#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
## more detail on manipulation check 

#put all the names, estimates, SEs, and behavior into one df for graphing 
Results_DF <- as.data.frame(matrix(data = c("Treatment Check", coeftest(Threat_1)[2, 1:2],"no", 
                                   "Treatment Check", coeftest(Threat_5)[2, 1:2], "yes",
                                   "Target Check", coeftest(Target_1)[2, 1:2],"no", 
                                   "Target Check", coeftest(Target_5)[2, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

colnames(Results_DF) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF$dv <- factor(Results_DF$dv, levels = c("Treatment Check", "Target Check"))
Results_DF$estimate <- as.numeric(Results_DF$estimate)
Results_DF$se <- as.numeric(Results_DF$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF$ci <- Results_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

ggplot(Results_DF, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Tweet Imagery ATE by MC or AC Question") +


#' 
#' 
## -----------------------------------------------------------------------------
## mean and se tables for threat check 

####Threat/Treatment Check 
Threat_Full_1 <- Threat_Check_Binary ~ 0 + as.factor(Tweet) 


lm_Threat_Full_1 <- lm(Threat_Full_1, 
                     data = DF_Follow2)

summary(lm_Threat_Full_1)
length(na.omit(DF_Follow2$Threat_Check_Binary[DF_Follow2$Tweet==1]))
length(na.omit(DF_Follow2$Threat_Check_Binary[DF_Follow2$Tweet==0]))


#' 
#' 
#' 
## -----------------------------------------------------------------------------
##### Target Check 
Target_Full_1 <- Target_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Target_Full_1 <- lm(Target_Full_1, 
                     data = DF_Follow2)

summary(lm_Target_Full_1)
length(na.omit(DF_Follow2$Target_Check_Binary[DF_Follow2$Tweet==1]))
length(na.omit(DF_Follow2$Target_Check_Binary[DF_Follow2$Tweet==0]))

#' 
#' 
#' ### Interaction Effects 
#' 
#' 
## -----------------------------------------------------------------------------

# looking at interactions between partisanship and tweet 
# and between age and Tweet 

### two new formulas for interaction. 8 is interaction with partisanship. 9 is for age 

Cred_Form_8 <- Credibility_Iran ~ Tweet*Q.Liberal + Tweet*Q.Conservative + 
  Q.Female + relevel(as.factor(Q.Race), ref = 1) + 
    Q.Age + Q.Bach + Q.Income  +  Q.Veteran + Q.Twitter_Some
  
Cred_Form_9 <- Credibility_Iran ~ Tweet*Q.Twitter_Some +
  Q.Female + relevel(as.factor(Q.Race), ref = 1) + 
    Q.Bach + Q.Income + Q.Conservative + Q.Liberal + 
    Q.Veteran + Q.Twitter_Some



Cred_8 <- lm(Cred_Form_8, data = DF_Follow2) 
Cred_9 <- lm(Cred_Form_9, data = DF_Follow2) 

stargazer(Cred_8, Cred_9, title = "Crediblity with Interaction Terms with Covariates", no.space = TRUE)




#' 
#' ## AC Sensitivity Analysis  
#' 
#' ### Just ACs   
#' 
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------
DF4 <- subset(DF_Follow2, DF_Follow2$Target_Check_Binary==1)


#' 
#' #### Analysis 
#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


Cred_Full_1 <- Credibility_Iran ~ 0 + as.factor(Tweet) 
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF4)

summary(lm_Cred_Full_1)

length(na.omit(DF4$Credibility_Iran[DF4$Tweet==1]))
length(na.omit(DF4$Credibility_Iran[DF4$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 

##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

Cred_Form_1 <- Credibility_Iran ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

Cred_Form_5 <- 
  Credibility_Iran ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))
  

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility_Iran ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Cred_1 <- lm(Cred_Form_1, 
             data = DF4, 
             na.action=na.omit)

Cred_5 <- lm(Cred_Form_5, 
             data = DF4, 
             na.action=na.omit)

Cred_7 <- lm(Cred_Form_7, 
             data = DF4, 
             na.action=na.omit)

library(stargazer)


stargazer(Cred_1, Cred_5, Cred_7, title = "Perceived Credibility, AC Question Correct", no.space = TRUE)


#' 
#' 
#' ### Just MCs   
#' 
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------
DF5 <- subset(DF_Follow2, DF_Follow2$Threat_Check_Binary==1)

#' 
#' #### Analysis 
#' 
#' 
#' 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


Cred_Full_1 <- Credibility_Iran ~ 0 + as.factor(Tweet) 
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF5)

summary(lm_Cred_Full_1)

length(na.omit(DF5$Credibility_Iran[DF5$Tweet==1]))
length(na.omit(DF5$Credibility_Iran[DF5$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 

##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

Cred_Form_1 <- Credibility_Iran ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

Cred_Form_5 <- 
  Credibility_Iran ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))
  

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility_Iran ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Cred_1 <- lm(Cred_Form_1, 
             data = DF5, 
             na.action=na.omit)

Cred_5 <- lm(Cred_Form_5, 
             data = DF5, 
             na.action=na.omit)

Cred_7 <- lm(Cred_Form_7, 
             data = DF5, 
             na.action=na.omit)

library(stargazer)


stargazer(Cred_1, Cred_5, Cred_7, title = "Perceived Credibility, MC Question Correct", no.space = TRUE)


#' 
#' 
#' # Follow On 3, Vetting 
#' 
#' 
## -----------------------------------------------------------------------------
DF_Follow3 <-read.csv("Data, Follow_3_Vetting.csv")

#' 
#' 
#' ## Demographic Table Stats 
#' 
#' 
## -----------------------------------------------------------------------------

### demographics 

table(DF_Follow3$Q.Female)

length(na.omit(DF_Follow3$Q.Female[DF_Follow3$Q.Female==0]))/length(DF_Follow3$Q.Female)
sum(na.omit(DF_Follow3$Q.Female))/length(DF_Follow3$Q.Female)
sum(is.na(DF_Follow3$Q.Female))/length(DF_Follow3$Q.Female)


#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow3$Q.White))/length((DF_Follow3$Q.White))
sum(na.omit(DF_Follow3$Q.Black))/length((DF_Follow3$Q.Black))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow3$Q.AIorAN))/length((DF_Follow3$Q.AIorAN))
sum(na.omit(DF_Follow3$Q.Asian))/length((DF_Follow3$Q.Asian))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow3$Q.NHorPI))/length((DF_Follow3$Q.NHorPI))
sum(na.omit(DF_Follow3$Q.Hispanic))/length((DF_Follow3$Q.Hispanic))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow3$Q.Mixed))/length((DF_Follow3$Q.Mixed))
sum(na.omit(DF_Follow3$Q.Other))/length((DF_Follow3$Q.Other))

#' 
#' 
## -----------------------------------------------------------------------------
sum(na.omit(DF_Follow3$Q.Other_Mixed))/length((DF_Follow3$Q.Other_Mixed))
sum(is.na(DF_Follow3$Q.Race))/length(DF_Follow3$Q.Race)




#' 
#' 
## -----------------------------------------------------------------------------
summary(DF_Follow3$Q.Age)

#' 
#' 
## -----------------------------------------------------------------------------


table(DF_Follow3$Q.Education)

sum((na.omit(DF_Follow3$Q.HighSchool)))/length((DF_Follow3$Q.HighSchool))
sum((na.omit(DF_Follow3$Q.Bach)))/length((DF_Follow3$Q.Bach))

sum(is.na(DF_Follow3$Q.Education))/length(DF_Follow3$Q.Education)


#' 
#' 
## -----------------------------------------------------------------------------
table(DF_Follow3$Q.Income)


length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==1]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==2]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==3]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==4]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==5]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==6]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==7]))/length(DF_Follow3$Q.Income)
length(na.omit(DF_Follow3$Q.Income[DF_Follow3$Q.Income==8]))/length(DF_Follow3$Q.Income)



sum(is.na(DF_Follow3$Q.Income))/length(DF_Follow3$Q.Income)


#' 
#' 
## -----------------------------------------------------------------------------


table(DF_Follow3$Q.Political_ID)

sum(na.omit(DF_Follow3$Q.Liberal))/length((DF_Follow3$Q.Liberal))
sum(na.omit(DF_Follow3$Q.Moderate))/length((DF_Follow3$Q.Moderate))
sum(na.omit(DF_Follow3$Q.Conservative))/length((DF_Follow3$Q.Conservative))


sum(is.na(DF_Follow3$Q.Political_ID))/length(DF_Follow3$Q.Political_ID)


#' 
#' 
## -----------------------------------------------------------------------------

table(DF_Follow3$Q.Veteran)
length(na.omit(DF_Follow3$Q.Veteran[DF_Follow3$Q.Veteran==0]))/length(DF_Follow3$Q.Veteran)
length(na.omit(DF_Follow3$Q.Veteran[DF_Follow3$Q.Veteran==1]))/length(DF_Follow3$Q.Veteran)

sum(is.na(DF_Follow3$Q.Veteran))/length(DF_Follow3$Q.Veteran)



#' 
#' 
## -----------------------------------------------------------------------------

#Twitter_Use 

table(DF_Follow3$Q.Twitter_Use)



length(na.omit(DF_Follow3$Q.Twitter_Use[DF_Follow3$Q.Twitter_Use==1]))/length(DF_Follow3$Q.Twitter_Use)
length(na.omit(DF_Follow3$Q.Twitter_Use[DF_Follow3$Q.Twitter_Use==2]))/length(DF_Follow3$Q.Twitter_Use)
length(na.omit(DF_Follow3$Q.Twitter_Use[DF_Follow3$Q.Twitter_Use==3]))/length(DF_Follow3$Q.Twitter_Use)
length(na.omit(DF_Follow3$Q.Twitter_Use[DF_Follow3$Q.Twitter_Use==4]))/length(DF_Follow3$Q.Twitter_Use)
length(na.omit(DF_Follow3$Q.Twitter_Use[DF_Follow3$Q.Twitter_Use==5]))/length(DF_Follow3$Q.Twitter_Use)
sum(is.na(DF_Follow3$Q.Twitter_Use))/length(DF_Follow3$Q.Twitter_Use)


#' 
#' 
#' 
#' 
#' ## Analysis 
#' 
#' ### Credibility 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


Cred_Full_1 <- Credibility ~ 0 + as.factor(Tweet) 
lm_Cred_Full_1 <- lm(Cred_Full_1, 
                     data = DF_Follow3)

summary(lm_Cred_Full_1)

length(na.omit(DF_Follow3$Credibility[DF_Follow3$Tweet==1]))
length(na.omit(DF_Follow3$Credibility[DF_Follow3$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 

##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

Cred_Form_1 <- Credibility ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

Cred_Form_5 <- 
  Credibility ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))
  

## Model 7: Slightly simplified for the Appendix 

Cred_Form_7 <- Credibility ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Cred_1 <- lm(Cred_Form_1, 
             data = DF_Follow3, 
             na.action=na.omit)

Cred_5 <- lm(Cred_Form_5, 
             data = DF_Follow3, 
             na.action=na.omit)

Cred_7 <- lm(Cred_Form_7, 
             data = DF_Follow3, 
             na.action=na.omit)

library(stargazer)


stargazer(Cred_1, Cred_5, Cred_7, title = "Perceived Credibility with Covariates", no.space = TRUE)


#' ### Vetting 
#' 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Vetting 


Vet_Full_1 <- Vetting ~ 0 + as.factor(Tweet) 
lm_Vet_Full_1 <- lm(Vet_Full_1, 
                     data = DF_Follow3)

summary(lm_Vet_Full_1)

length(na.omit(DF_Follow3$Vetting[DF_Follow3$Tweet==1]))
length(na.omit(DF_Follow3$Vetting[DF_Follow3$Tweet==0]))

#' 
#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 

##DV: Vetting, IV: Tweet binary  

## Model 1: No Demographics 

Vet_Form_1 <- Vetting ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

Vet_Form_5 <- 
  Vetting ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))
  

## Model 7: Slightly simplified for the Appendix 

Vet_Form_7 <- Vetting ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Vet_1 <- lm(Vet_Form_1, 
             data = DF_Follow3, 
             na.action=na.omit)

Vet_5 <- lm(Vet_Form_5, 
             data = DF_Follow3, 
             na.action=na.omit)

Vet_7 <- lm(Vet_Form_7, 
             data = DF_Follow3, 
             na.action=na.omit)

library(stargazer)


stargazer(Vet_1, Vet_5, Vet_7, title = "Perceived Vetting with Covariates", no.space = TRUE)


#' 
#' 
#' 
## -----------------------------------------------------------------------------

#Credibility and vetting 



Cred_Alone <- as.data.frame(matrix(data = c("credibility", coeftest(Cred_5)[2, 1:2],"Yes", 
                                   "credibility", coeftest(Cred_1)[2, 1:2], "No",
                                   "vetting", coeftest(Vet_5)[2, 1:2],"Yes", 
                                   "vetting", coeftest(Vet_1)[2, 1:2], "No"), 
                                   ncol = 4, byrow = TRUE))

colnames(Cred_Alone) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Cred_Alone$dv <- factor(Cred_Alone$dv, levels = c("credibility", "vetting"))
Cred_Alone$estimate <- as.numeric(Cred_Alone$estimate)
Cred_Alone$se <- as.numeric(Cred_Alone$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Cred_Alone$ci <- Cred_Alone$se*q

library(ggplot2)
## graph time 
pd <- position_dodge(0.5)

ggplot(Cred_Alone, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Credibility, ATE of Tweet Medium, Iran")

#' 
#' 
## -----------------------------------------------------------------------------

#Credibility and vetting 



Cred_Alone <- as.data.frame(matrix(data = c("vetting", coeftest(Vet_5)[2, 1:2],"Yes", 
                                   "vetting", coeftest(Vet_1)[2, 1:2], "No"), 
                                   ncol = 4, byrow = TRUE))

colnames(Cred_Alone) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Cred_Alone$dv <- factor(Cred_Alone$dv, levels = c("vetting"))
Cred_Alone$estimate <- as.numeric(Cred_Alone$estimate)
Cred_Alone$se <- as.numeric(Cred_Alone$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Cred_Alone$ci <- Cred_Alone$se*q

library(ggplot2)
## graph time 
pd <- position_dodge(0.5)

ggplot(Cred_Alone, aes(x = controls, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Controls Present?") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Credibility, ATE of Tweet Medium, Iran")

#' 
#' 
#' 
#' 
#' 
#' 
#' 
#' ### Manipulation Check 
#' 
#' 
## -----------------------------------------------------------------------------
# Target Check 

## Model 1: No Demographics 

Target_Form_1 <- Target_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Target_Form_5 <- Target_Check_Binary ~ Tweet +
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Target_Form_7 <- Target_Check_Binary ~ Tweet +
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Target_1 <- lm(Target_Form_1, 
             data = DF_Follow3, 
             na.action=na.omit)

Target_5 <- lm(Target_Form_5, 
             data = DF_Follow3, 
             na.action=na.omit)

Target_7 <- lm(Target_Form_7, 
             data = DF_Follow3, 
             na.action=na.omit)


stargazer(Target_1, Target_5, Target_7, title = "Target Check Question with Covariates", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
# Support Check 

## Model 1: No Demographics 

Support_Form_1 <- Support_Check_Binary ~ Tweet 

  
## Model 5: Factor Demographics 

Support_Form_5 <- Support_Check_Binary ~ Tweet + 
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Support_Form_7 <- Support_Check_Binary ~ Tweet +
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Support_1 <- lm(Support_Form_1, 
             data = DF_Follow3, 
             na.action=na.omit)

Support_5 <- lm(Support_Form_5, 
             data = DF_Follow3, 
             na.action=na.omit)

Support_7 <- lm(Support_Form_7, 
             data = DF_Follow3, 
             na.action=na.omit)


stargazer(Support_1, Support_5, Support_7, title = "Support Check Question with Covariates", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
# Sea Check 

## Model 1: No Demographics 

Sea_Form_1 <- Sea_Check_Binary ~ Tweet    

  
## Model 5: Factor Demographics 

Sea_Form_5 <- Sea_Check_Binary ~ Tweet +
  Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) 




## Model 7: Slightly simplified for the Appendix 

Sea_Form_7 <- Sea_Check_Binary ~ Tweet +
  Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Sea_1 <- lm(Sea_Form_1, 
             data = DF_Follow3, 
             na.action=na.omit)

Sea_5 <- lm(Sea_Form_5, 
             data = DF_Follow3, 
             na.action=na.omit)

Sea_7 <- lm(Sea_Form_7, 
             data = DF_Follow3, 
             na.action=na.omit)


stargazer(Sea_1, Sea_5, Sea_7, title = "Sea Check Question with Covariates", no.space = TRUE)

#' 
#' 
## -----------------------------------------------------------------------------
## more detail on manipulation check 

## ATE of Tweet Treatment 

#put all the names, estimates, SEs, and behavior into one df for graphing 
Results_DF <- as.data.frame(matrix(data = c("Target Check", coeftest(Target_1)[2, 1:2],"no", 
                                   "Target Check", coeftest(Target_5)[2, 1:2], "yes",
                                   "Support Check", coeftest(Support_1)[2, 1:2], "no",
                                   "Support Check", coeftest(Support_5)[2, 1:2], "yes",
                                   "Sea Check", coeftest(Sea_1)[2, 1:2], "no",
                                   "Sea Check", coeftest(Sea_5)[2, 1:2], "yes"), 
                                   ncol = 4, byrow = TRUE))

colnames(Results_DF) <- c("dv", "estimate", "se", "controls")

#making into correct operators 
Results_DF$dv <- factor(Results_DF$dv, levels = c("Target Check", "Support Check", "Sea Check"))
Results_DF$estimate <- as.numeric(Results_DF$estimate)
Results_DF$se <- as.numeric(Results_DF$se)

#adding in CIs 
q <- as.numeric(qnorm(p=.05/2, lower.tail=FALSE))
Results_DF$ci <- Results_DF$se*q


## graph time 
library(ggplot2)
pd <- position_dodge(0.5)

ggplot(Results_DF, aes(x = dv, y = estimate, color = controls))  + 
  geom_point(aes(color=controls, shape=controls), position = pd) +
  geom_errorbar(aes(ymin = estimate - ci, ymax = estimate + ci), width = .2, position = pd) +
  theme_minimal() + xlab("Question") + ylab("ATE") + 
  geom_hline(yintercept = 0, linetype="dotted") +
  theme(axis.text.x = element_text(hjust = 1), text = element_text(size = 14)) + coord_flip()

#ggtitle("Tweet Imagery ATE by MC or AC Question") +


#' 
#' 
## -----------------------------------------------------------------------------
## mean and se tables  

##### Target Check 
Target_Full_1 <- Target_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Target_Full_1 <- lm(Target_Full_1, 
                     data = DF_Follow3)

summary(lm_Target_Full_1)
length(na.omit(DF_Follow3$Target_Check_Binary[DF_Follow3$Tweet==1]))
length(na.omit(DF_Follow3$Target_Check_Binary[DF_Follow3$Tweet==0]))

#' 
#' 
## -----------------------------------------------------------------------------
##### Support Check 
Support_Full_1 <- Support_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Support_Full_1 <- lm(Support_Full_1, 
                     data = DF_Follow3)

summary(lm_Support_Full_1)
length(na.omit(DF_Follow3$Support_Check_Binary[DF_Follow3$Tweet==1]))
length(na.omit(DF_Follow3$Support_Check_Binary[DF_Follow3$Tweet==0]))

#' 
#' 
#' 
## -----------------------------------------------------------------------------
##### Sea Check 
Sea_Full_1 <- Sea_Check_Binary ~ 0 + as.factor(Tweet) 

lm_Sea_Full_1 <- lm(Sea_Full_1, 
                     data = DF_Follow3)

summary(lm_Sea_Full_1)
length(na.omit(DF_Follow3$Sea_Check_Binary[DF_Follow3$Tweet==1]))
length(na.omit(DF_Follow3$Sea_Check_Binary[DF_Follow3$Tweet==0]))

#' 
#' 
#' ## AC Sensitivity Analysis  
#' 
#' ### Just three ACs   
#' 
#' 
#' #### Subset 
#' 
## -----------------------------------------------------------------------------
DF4 <- subset(DF_Follow3, DF_Follow3$Target_Check_Binary==1)
DF5 <- subset(DF4, DF4$Support_Check_Binary==1)
DF6 <- subset(DF5, DF5$Sea_Check_Binary==1)

#' 
#' #### Analysis 
#' 
#' 
#' ##### Credibility 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Credibility 


AC.Cred_Full_1 <- Credibility ~ 0 + as.factor(Tweet) 
AC.lm_Cred_Full_1 <- lm(AC.Cred_Full_1, 
                     data = DF6)

summary(AC.lm_Cred_Full_1)

length(na.omit(DF6$Credibility[DF6$Tweet==1]))
length(na.omit(DF6$Credibility[DF6$Tweet==0]))

#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 


##DV: credibility, IV: Tweet binary  

## Model 1: No Demographics 

AC.Cred_Form_1 <- Credibility ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

AC.Cred_Form_5 <- 
  Credibility ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran 
  

## Model 7: Slightly simplified for the Appendix 

AC.Cred_Form_7 <- Credibility ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran

#regression 

AC.Cred_1 <- lm(AC.Cred_Form_1, 
             data = DF6, 
             na.action=na.omit)

AC.Cred_5 <- lm(AC.Cred_Form_5, 
             data = DF6, 
             na.action=na.omit)

AC.Cred_7 <- lm(AC.Cred_Form_7, 
             data = DF6, 
             na.action=na.omit)

library(stargazer)


stargazer(AC.Cred_1, AC.Cred_5, AC.Cred_7, title = "Perceived Credibility, AC Questions Correct", no.space = TRUE)


#' 
#' ##### Vetting 
#' 
#' 
## -----------------------------------------------------------------------------
## a simple regression for mean and se estimates 


######## Vetting 


Vet_Full_1 <- Vetting ~ 0 + as.factor(Tweet) 
lm_Vet_Full_1 <- lm(Vet_Full_1, 
                     data = DF6)

summary(lm_Vet_Full_1)

length(na.omit(DF_Follow3$Vetting[DF_Follow3$Tweet==1]))
length(na.omit(DF_Follow3$Vetting[DF_Follow3$Tweet==0]))

#' 
#' 
## ---- results='hide'----------------------------------------------------------
#time for some regressions 

#let's do a couple different models 

##DV: Vetting, IV: Tweet binary  

## Model 1: No Demographics 

Vet_Form_1 <- Vetting ~ Tweet   

## Model 2: Demographics in Erik's Paper 


## Model 5: Factor Demographics 

Vet_Form_5 <- 
  Vetting ~ Tweet + Q.Female +
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.HighSchool + Q.Bach + 
  factor(Q.Income, ordered = TRUE, levels = c(1, 2, 3, 4, 5, 6, 7, 8)) +  #treats income as an ordered factor
  factor(Q.Political_ID, ordered = TRUE, levels = c(1, 2, 3, 4, 5)) + #treats party as an ordered factor 
  Q.Veteran + factor(Q.Twitter_Use, ordered = TRUE, levels = c(1, 2, 3, 4, 5))
  

## Model 7: Slightly simplified for the Appendix 

Vet_Form_7 <- Vetting ~ Tweet + Q.Female + 
  relevel(as.factor(Q.Race), ref = 1) + #makes white the reference cat
  Q.Age + Q.Bach + Q.Income +  #treats income as a continuous variable, removes high school dummy 
  Q.Conservative + Q.Liberal + #uses party binaries 
  Q.Veteran + Q.Twitter_Some

#regression 

Vet_1 <- lm(Vet_Form_1, 
             data = DF6, 
             na.action=na.omit)

Vet_5 <- lm(Vet_Form_5, 
             data = DF6, 
             na.action=na.omit)

Vet_7 <- lm(Vet_Form_7, 
             data = DF6, 
             na.action=na.omit)

library(stargazer)


stargazer(Vet_1, Vet_5, Vet_7, title = "Perceived Vetting, All AC Questions Correct", no.space = TRUE)


#' 
#' 
#' 
#' # Convert File 
#' 
## ---- include = FALSE---------------------------------------------------------
library(knitr)
purl("Code, Cheap Tweets.rmd", output = "Code, Cheap Tweets.r", documentation = 2)

#' 
